register("pedro.event");
require("pedro.element","pedro.event");

pedro.event.createEventListener = function(func,prevDefault){
    return function(e){
        var evt = (e) ? e : event;
        
        if (prevDefault) {
          if (evt.preventDefault) // For Moz.
            evt.preventDefault();
          func(evt);
          return false;
        }
        
        return func(evt);
    }
}

pedro.event.addEventListener = function(el,evt,func,capture,prevDefault){
    var listener = pedro.event.createEventListener(func,prevDefault);
    if(!isUndef(el.attachEvent)){
        evt = "on"+evt;
        if((el == window) && (evt != 'onload') && (evt != 'onmouseout')){
            d.attachEvent(evt,listener);
        }else{
            el.attachEvent(evt,listener);
        }
    }
    else if(!isUndef(el.addEventListener)){
        el.addEventListener(evt,listener,capture);
    }
    return listener;
}

pedro.event.removeEventListener = function(el,evt,listener,capture){
    if(!isUndef(el.attachEvent)){
        evt = "on"+evt;
        if((el == window) && (evt != 'onload') && (evt != 'onmouseout')){
            d.detachEvent(evt,listener);
        }else{
            el.detachEvent(evt,listener);
        }
    }
    else if(!isUndef(el.addEventListener)){
        el.removeEventListener(evt,listener,capture);
    }
}


pedro.event.executeOnLoad = function(){
    for(var i=0; i<this.queue.length; i++){
        if(isString(this.queue[i])) eval(this.queue[i]);
        else this.queue[i].apply();
    }
    for(var i=0; i<this.queueLast.length; i++){
        if(isString(this.queueLast[i])) eval(this.queueLast[i]);
        else this.queueLast[i].apply();
    }
}

pedro.event.queue = new Array();

pedro.event.addToOnLoad = function(func){ this.queue.push(func); }

pedro.event.queueLast = new Array();

pedro.event.addToOnLoadLast = function(func){ this.queueLast.push(func); }

function getEventType(evt){ return evt.type; }

function getEventTarget(evt){ return (evt.target) ? evt.target : evt.srcElement; }

function getEventX(evt){ return (evt.clientX) ? evt.clientX : evt.offsetX; }

function getEventY(evt){ return (evt.clientY) ? evt.clientY : evt.offsetY; }

function getEventPos(evt){
    return new Coord(getEventX(evt),getEventY(evt));
}


/* To Track the Mouse */
/* Hope it doesn't break dragging */

function handleMouse(evt){
    try{
        pedro.event.mousePos = getEventPos(evt);
    }
    catch(e){}
}

pedro.event.addEventListener(window,"mousemove",handleMouse,false);

pedro.event.mousePos = new Coord(null,null);


/* Don't mess with the Magic */
/* Thanks Dean @ http://dean.edwards.name */

/* Mozilla */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", function(){ pedro.event.executeOnLoad(); }, false);
}

/* IE */
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
    if (this.readyState == "complete") {
        pedro.event.executeOnLoad(); // call the onload handler
    }
};
/*@end @*/

/* Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            clearInterval(_timer);
            pedro.event.executeOnLoad(); // call the onload handler
        }
    }, 10);
}
